import { hasActionPriv } from '@/utils/perm'

function checkPerm(el, binding) {
  const { value } = binding
  if (value && value instanceof Array) {
    if (value.length > 0) {
      const hasPriv = hasActionPriv(value)
      if (!hasPriv) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  } else {
    throw new Error(`you have no access permission.`)
  }
}

export default {
  inserted(el, binding) {
    checkPerm(el, binding)
  },
  update(el, binding) {
    checkPerm(el, binding)
  }
}
